﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using SolidQ.CommonLibraries.GestionErrores;


namespace SolidQSqlConnection
{

    public static class T_SQLScriptWorker
    {
        public static object ExecuteScalar(SqlConnection conn, int connectionTimeout, string query)
        {
            try
            {
                ///// We need to replace all the internal script variables before his execution
                ///// 
                //query = ReplaceScriptVariables(query);

                SqlCommand command = new SqlCommand(query, conn);
                command.CommandType = System.Data.CommandType.Text;
                command.CommandTimeout = connectionTimeout;

                return (command.ExecuteScalar());

            }
            catch (Exception e)
            {
                string textoCorto = e.Message;
                string textoLargo = query;
                throw new SolidQException(textoCorto, textoLargo, e.InnerException);
            }
        }
        public static DataTable GetDataTable(SqlConnection conn, int connectionTimeout, string query)
        {
            try
            {
                DataTable tableReturn = new DataTable();

                SqlCommand command = new SqlCommand(query, conn);
                command.CommandType = System.Data.CommandType.Text;
                command.CommandTimeout = conn.ConnectionTimeout;

                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(tableReturn);
                return tableReturn;
            }
            catch (Exception e)
            {
                string textoCorto = e.Message;
                string textoLargo = query;
                throw new SolidQException(textoCorto, textoLargo, e.InnerException);
            }

        }
    }
}

